package com.slmxz.tms.system.controller;

import com.slmxz.tms.system.service.CompanyService;
import com.slmxz.tms.common.support.Condition;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.slmxz.tms.system.model.vo.CompanyVO;
import com.slmxz.tms.system.model.entity.Company;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.slmxz.tms.common.result.PageResult;
import com.slmxz.tms.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import com.slmxz.tms.common.model.Query;

/**
 * 公司管理前端控制层
 * @author sunyao
 * @Date 2024-11-08
 */
@Tag(name = "公司管理接口")
@RestController
@RequestMapping("/system/company")
@RequiredArgsConstructor
public class CompanyController  {

    private final CompanyService companyService;

    @Operation(summary = "公司管理分页列表")
    @GetMapping("/page")
    public PageResult<CompanyVO> selectCompanyPage(Query query,CompanyVO company) {
        IPage<CompanyVO> result = this.companyService.selectCompanyPage(Condition.getPage(query),company);
        return PageResult.success(result);
    }



    @Operation(summary = "新增公司管理")
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPerm('system:company:add')")
    public Result<Void> saveCompany(@RequestBody @Valid CompanyVO  company) {
        boolean result = this.companyService.saveCompany(company);
        return Result.result(result);
    }

    @Operation(summary = "获取公司数据作为下拉框来源")
    @GetMapping("/options")
    public Result<?> companyOptions(CompanyVO company) {
        return Result.success(this.companyService.options(company));
    }

    @Operation(summary = "获取公司管理详情数据")
    @GetMapping("/detail/{id}")
    @PreAuthorize("@ss.hasPerm('system:company:edit')")
    public Result<Company> selectCompanyDetail(
        @Parameter(description = "公司管理ID") @PathVariable Long id
    ) {
        Company company = this.companyService.selectCompanyDetail(id);
        return Result.success(company);
    }

    @Operation(summary = "修改公司管理")
    @PutMapping(value = "/update")
    @PreAuthorize("@ss.hasPerm('system:company:edit')")
    public Result<Void> updateCompany(
            @RequestBody @Validated CompanyVO company
    ) {
        boolean result = companyService.updateCompany(company);
        return Result.result(result);
    }

    @Operation(summary = "删除公司管理")
    @DeleteMapping("/remove/{ids}")
    @PreAuthorize("@ss.hasPerm('system:company:delete')")
    public Result<Void> deleteCompanys(
        @Parameter(description = "公司管理ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = this.companyService.deleteCompanys(ids);
        return Result.result(result);
    }

    @Operation(summary = "激活或者注销公司管理")
    @PostMapping("/activeOrLogout")
    @PreAuthorize("@ss.hasPerm('system:company:delete')")
    public Result<Void> activeOrLogout(@RequestBody CompanyVO company) {
        boolean result = this.companyService.activeOrLogout(company);
        return Result.result(result);
    }
}
